package com.lc1993929.jpa;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

/**
 * Created by LiuChang on 2017/6/1.
 */
public class JPAUtils {
    //实现EntityManagerFactory在当前项目只有一个实例。单例
    private static EntityManagerFactory entityManagerFactory;

    //先执行静态属性，才是静态块
    static {
        try {
            entityManagerFactory = Persistence.createEntityManagerFactory("com.lc1993929.jpa");
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("解析配置文件或持久对象配置出现异常：" + e.getMessage());
        }
    }

    //对外提供静态方法，获取EntityManager
    public static EntityManager getEntityManager() {
        return entityManagerFactory.createEntityManager();
    }

}
